import smtplib
from email.mime.text import MIMEText
from email.header import Header


def notice(config, text):
    sender = config.sender
    receivers = config.receivers
    host = config.host
    port = config.port
    password = config.password
    message = MIMEText(text, 'plain', 'utf-8')
    message['From'] = Header("基金提醒", 'utf-8')  # 发送者
    message['To'] = Header("敬爱的基金关注者", 'utf-8')  # 接收者
    subject = '基金提醒通知...'
    message['Subject'] = Header(subject, 'utf-8')
    try:
        smtp = smtplib.SMTP_SSL(host=host, port=int(port))
        smtp.set_debuglevel(1)
        smtp.login(user=sender, password=password)
        smtp.sendmail(from_addr=sender, to_addrs=receivers, msg=message.as_string())
        print("邮件发送成功")
        smtp.quit()
    except smtplib.SMTPException as e:
        print(f"Error: {e}")
